package com.trust.pams.dao.service.impl;

import com.chasonx.common.model.PageResult;
import com.mongodb.client.result.DeleteResult;
import com.trust.pams.dao.mongo.BaseMongoDaoImpl;
import com.trust.pams.dao.service.PamsClientDao;
import com.trust.pams.dao.service.PamsPuoteDao;
import com.trust.pams.dto.PamsClientDto;
import com.trust.pams.dto.PamsQuoteDto;
import com.trust.pams.model.query.PamsClientQuery;
import com.trust.pams.util.DataUtil;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class PamsClientDaoImpl extends BaseMongoDaoImpl<PamsClientDto> implements PamsClientDao {

    @Override
    public int batchPamsClient(List<PamsClientDto> pamsquote) {
        super.insertAll(pamsquote);
        return 1;
    }

    @Override
    public DeleteResult deletePamsClient(String clientName) {
        Query query = new Query();
        Criteria criteria = Criteria.where("clientName").is(clientName);
        query.addCriteria(criteria);
        return super.getMongoTemplate().remove(query, PamsClientDto.class);
    }

    @Override
    public List<PamsClientDto> getPamsClient(String clientName) {
        Query dbQuery = new Query();
        dbQuery.addCriteria(Criteria.where("clientName").is(clientName));
        List<PamsClientDto> listData = find(dbQuery);
        return listData;
    }

    @Override
    public PageResult<PamsClientDto> listPamsClient(PamsClientQuery pamsClientQuery) {
        Query dbQuery = new Query();
        if(DataUtil.isNotEmpty(pamsClientQuery.getClientName())){
            dbQuery.addCriteria(Criteria.where("clientName").is(pamsClientQuery.getClientName()));
        }
        long count = getMongoTemplate().count(dbQuery, getCollectionName());
        dbQuery.skip( (pamsClientQuery.getPageNumber() - 1) * pamsClientQuery.getPageSize()).limit(pamsClientQuery.getPageSize());

        List<PamsClientDto> listData = find(dbQuery);

        PageResult<PamsClientDto> page = new PageResult<>();
        page.setTotalRow(count);
        page.setData(listData);
        return page;
    }

    @Override
    public List<PamsClientDto> allPamsClient() {
        List<PamsClientDto> listData = findAll();
        return listData;
    }
}
